import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Form/index';
import type { Rule } from 'ant-design-vue/es/form';
// import { getDBListApi } from '/@/api/database/account';
import { verify } from '/@/utils/validator';
const zhChecked = async (_rule: Rule, value: string) => {
  if (verify.includesZh.value.test(value)) {
    return Promise.reject('只能输入非中文字符');
  }
  return Promise.resolve();
};

const resourceList = [
  { label: '接口资源', value: 0 },
  { label: '文件资源', value: 1 },
];

const StoreTypeList = [
  {
    label: '缺省',
    value: 0,
  },
  {
    label: '按月',
    value: 1,
  },
  {
    label: '按年',
    value: 2,
  },
  {
    label: '数据库',
    value: 3,
  },
];
enum ResourceCode {
  API,
  SQL,
  FILE,
}

const isShowAPI = (code) => {
  return ResourceCode.API === code;
};

export const columns: BasicColumn[] = [
  {
    title: '资源类型',
    dataIndex: 'classify',
    align: 'left',
    customRender: ({ record }) => {
      const obj = resourceList.find((i) => i.value === record.classify);
      return obj && obj.label;
    },
  },

  {
    title: '资源编码',
    dataIndex: 'code',
    align: 'left',
  },
  {
    title: '资源名称',
    dataIndex: 'name',
    align: 'left',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'left',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '资源名称',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'classify',
    label: '资源类型',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: resourceList,
    },
  },
  {
    field: 'code',
    label: '资源编码',
    component: 'Input',
    colProps: { span: 4 },
  },
];

export const formSchemas: FormSchema[] = [
  // 接口类型
  {
    field: 'classify',
    label: '资源类型',
    component: 'Select',
    required: true,
    defaultValue: 0,
    componentProps: {
      options: resourceList,
    },
  },
  {
    field: 'code',
    label: '资源编码',
    component: 'Input',
    required: true,
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
  },
  {
    field: 'name',
    label: '编码名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'apiType',
    label: '接口类型',
    component: 'Select',
    required: true,
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '普通接口', value: '0' },
        { label: '文件上传', value: '1' },
        { label: '文件下载（导出）', value: '2' },
      ],
    },
    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'verb',
    label: 'HTTP请求谓词',
    component: 'Select',
    componentProps: {
      options: [
        { label: 'GET', value: 'GET' },
        { label: 'POST', value: 'POST' },
      ],
    },
    required: true,
    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'url',
    label: '请求URL',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'params',
    label: '请求参数',
    component: 'InputTextArea',

    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'body',
    label: '请求参数体',
    component: 'InputTextArea',

    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'callbackName',
    label: '回调函数名',
    component: 'Input',
    ifShow: ({ values }) => isShowAPI(values.classify),
  },
  {
    field: 'summary',
    label: '说明',
    component: 'InputTextArea',
  },

  // 文件上传
  // {
  //   field: 'dataSourceType',
  //   label: '数据来源',
  //   component: 'ApiSelect',
  //   required: true,
  //   componentProps: () => {
  //     return {
  //       api: async (params) => {
  //         const ret = await getDBListApi(params);
  //         ret.forEach((e) => {
  //           e.connName = `${e.connName} (${e.connCode})`;
  //         });
  //         // @ts-ignore
  //         ret.unshift({ connName: '固定数据', connCode: '-1' });
  //         // @ts-ignore
  //         ret.unshift({ connName: '数据字典', connCode: '-2' });

  //         return ret;
  //       },
  //       showSearch: true,
  //       filterOption: (input: string, option: any) => {
  //         return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  //       },
  //       immediate: true,
  //       allowClear: false,
  //       placeholder: '请选择数据来源',
  //       labelField: 'connName',
  //       valueField: 'connCode',
  //       getPopupContainer: () => document.body,
  //     };
  //   },
  //   ifShow: ({ values }) => !isShowAPI(values.classify),
  // },

  {
    field: 'primaryName',
    label: '主键名称',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'primaryCode',
    label: '主键编码',
    component: 'Input',
    required: true,
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },

  {
    field: 'foreignName',
    label: '外键名称',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'foreignCode',
    label: '外键编码',
    component: 'Input',
    required: true,
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },

  {
    field: 'serialNumberName',
    label: '排序字段',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'serialNumberCode',
    label: '排序字段编码',
    component: 'Input',
    required: true,
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },

  {
    field: 'attachmentName',
    label: '文件名称',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'attachmentCode',
    label: '文件编码',
    component: 'Input',
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'extName',
    label: '后缀名称',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'extCode',
    label: '后缀名编码',
    component: 'Input',
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },

  {
    field: 'tableName',
    label: '存储表名',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },

  {
    field: 'filePath',
    label: '文件存储路径',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'storeRule',
    label: '存储规则',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'limitCondition',
    label: '限制条件',
    component: 'Input',
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'storeType',
    label: '存储方式',
    component: 'RadioGroup',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: StoreTypeList,
    },
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'typeCode',
    label: '文件类型编码',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'typeName',
    label: '文件类型',
    component: 'Input',
    required: false,
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'contentName',
    label: '文件描述',
    component: 'Input',
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'contentCode',
    label: '文件描述编码',
    component: 'Input',
    rules: [
      {
        validator: zhChecked,
        trigger: 'blur',
      },
    ],
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'extField',
    label: '扩展字段',
    component: 'Input',
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
  {
    field: 'extFieldVal',
    label: '缺省值',
    component: 'Input',
    ifShow: ({ values }) => !isShowAPI(values.classify),
  },
];
